package com.bnyy.medicalHousekeeper.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.common.bean.UserInfo;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.activity.CustomerDetailActivity;
import com.bnyy.medicalHousekeeper.activity.ShopDetailActivity;
import com.bnyy.medicalHousekeeper.activity.SubordinateDetailActivity;
import com.bnyy.medicalHousekeeper.bean.Shop;
import com.bnyy.medicalHousekeeper.bean.Customer;
import com.bnyy.medicalHousekeeper.bean.Subordinate;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/8
 **/
public class IndexUserAdapter extends BaseNormalListAdapter<UserInfo, IndexUserAdapter.ViewHolder> {

    public IndexUserAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_index_user, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        UserInfo userInfo = getData(position);

//        if (userInfo instanceof Customer) {
//            if (((Customer) userInfo).isHousekeeper()) {
//                holder.tvTag.setVisibility(View.VISIBLE);
//            } else {
//                holder.tvTag.setVisibility(View.GONE);
//            }
//        }

        if (userInfo.getRole_id() == 3) {
            Shop shop = (Shop) userInfo;
            holder.tvName.setText(shop.getShop_name());
            GlideHelper.setCircleImage(mContext, shop.getShop_image(), holder.ivHeader, R.mipmap.icon_default_header_man);
        } else {
            holder.tvName.setText(userInfo.getUser_name());
            GlideHelper.setCircleImage(mContext, userInfo.getUser_image(), holder.ivHeader, R.mipmap.icon_default_header_man);
        }


        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userInfo instanceof Customer) {
                    CustomerDetailActivity.show(mContext, userInfo.getId());
                } else if (userInfo instanceof Subordinate) {
                    SubordinateDetailActivity.show(mContext, userInfo.getId());
                } else if (userInfo instanceof Shop) {
                    ShopDetailActivity.show(mContext, (Shop) userInfo);
                }
            }
        });
    }

    static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        ImageView ivHeader;
        TextView tvName;
        TextView tvTag;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            ivHeader = itemView.findViewById(R.id.iv_header);
            tvName = itemView.findViewById(R.id.tv_name);
            tvTag = itemView.findViewById(R.id.tv_tag);
        }
    }
}
